/**
 * @author yxw
 * date : 2013年10月12日 下午3:12:33 
 */
package com.tzinfo.tztbase;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.galhttprequest.GalHttpRequest;
import com.galhttprequest.GalHttpRequest.GalHttpLoadTextCallBack;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tzinfo.data.TztData;
import com.tzinfo.data.UserInfo;
import com.tzinfo.tzt.R;
import com.tzinfo.tztbase.util.DataSaveTools;
import com.yxw.util.OutLog;
import com.yxw.view.YXWProgressDialog;
import com.yxw.view.util.ResFinder;

public class TztBaseLoginActivity extends Activity {

	private Dialog dialog;
	private UserInfo userInfo;
	private String username = null;
	private String password = null;

	// UI组件
	private EditText uNameEditText, pwdEditText;
	private CheckBox check_auto_login;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tztbase_ac_login
        );
		init();
	}

	private void init() {

		uNameEditText = (EditText) ResFinder.findViewById(this,
				"R.id.et_username");
		pwdEditText = (EditText) ResFinder.findViewById(this,
				"R.id.et_password");
		check_auto_login = (CheckBox) ResFinder.findViewById(this,
				"R.id.check_auto_login");
		dialog = new YXWProgressDialog(this).createDialog("加载中…");
	}

	// onclick
	public void bt_back(View v) {
		this.finish();
	}

	// onclick
	public void bt_login(View v) {
		login();
	}

	// onclick
	public void bt_regist(View v) {
		Intent intent = new Intent(this, TztBaseRegistActivity.class);
		startActivityForResult(intent, 1);
	}

	public void login() {
		dialog.show();
		username = uNameEditText.getText().toString().trim();
		password = pwdEditText.getText().toString().trim();
		if (!TztData.checkLoginSubmitInfo(username, password)) {
			Toast.makeText(this, "请先输入帐号和密码！", Toast.LENGTH_SHORT).show();
			dialog.dismiss();
			return;
		}
		GalHttpRequest galHttpRequest = new GalHttpRequest(
				TztBaseLoginActivity.this);
		galHttpRequest.setCacheEnable(false);
		String url = "http://member.tzinfo.net/mob/api/Login.ashx"
				+ "?usertype=0&" + "username=" + username + "&" + "password="
				+ password;
		galHttpRequest.setURL(url);
		OutLog.LogE("url", url);
		galHttpRequest.startAsynRequestString(new GalHttpLoadTextCallBack() {

			@Override
			public void textLoaded(String arg0) {
				OutLog.LogE("JsonString", arg0 + "");
				Gson gson = new Gson();
				java.lang.reflect.Type type = new TypeToken<UserInfo>() {
				}.getType();
				userInfo = gson.fromJson(arg0, type);
				OutLog.LogE("userInfo", userInfo.toString());
				if (userInfo.getSuccess()) {
					Intent intent = new Intent();
					intent.putExtra(TztData.USER_ISLOGIN, true);
					TztData.isUserLogin = true;
					TztData.userInfo = userInfo;
					setResult(2, intent);
					Toast.makeText(getApplicationContext(), "恭喜，登录成功！", Toast.LENGTH_SHORT)
							.show();
					if (check_auto_login.isChecked()) {
						DataSaveTools.saveLoginInfo(TztBaseLoginActivity.this,
								username, password);
					} else {
						DataSaveTools.clearLoginInfo(TztBaseLoginActivity.this);
					}
					TztBaseMainActivity.requestUserPosition(TztBaseLoginActivity.this);
					dialog.dismiss();
					finish();
				} else {
					DataSaveTools.clearLoginInfo(TztBaseLoginActivity.this);
					getDataError(userInfo.getMsg());
				}
			}
		});

	}

	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton("取消", new DialogInterface.OnClickListener() {
					public void onClick

					(DialogInterface dialog, int which) {
						finish();
					}
				})
				.setNegativeButton("重试", new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						login();
					}
				}).show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1) {
			if (resultCode == 2) {
				Intent intent = new Intent();
				intent.putExtra(TztData.USER_ISLOGIN,
						data.getBooleanExtra(TztData.USER_ISLOGIN, false));
				setResult(2, intent);
				Toast.makeText(this, "恭喜，注册成功！", Toast.LENGTH_SHORT).show();
				finish();
			}
		}
	}

}
